package com.example.jl.bethinner.activity;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.jl.bethinner.R;
import com.squareup.timessquare.CalendarPickerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;


public class TestTimesSquareActivity {

    Activity activity;
    View v;
    private CalendarPickerView calendar;
    private SimpleDateFormat sdf;

    protected View onCreate(Activity activity) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_times_square);
        this.activity = activity;
        LayoutInflater li = activity.getLayoutInflater();
        v = li.inflate(R.layout.activity_times_square, null);
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.MONTH, 1);
        sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        calendar = (CalendarPickerView) v.findViewById(R.id.calendar_view);
        Date date1 = new Date(Long.valueOf("1468339200000"));
        Date date2 = new Date(Long.valueOf("1469030400000"));
        Date date3 = new Date(Long.valueOf("1468857600000"));

        Collection<Date> dates = new ArrayList<>();
        dates.add(date1);
        dates.add(date2);
        dates.add(date3);
        try {
            Date minDate = sdf.parse("2016-7-01");
            Date maxDate = sdf.parse("2016-8-0");
            calendar.init(minDate, maxDate)
                    .inMode(CalendarPickerView.SelectionMode.MULTIPLE)
                    .withSelectedDates(dates);

        } catch (ParseException e) {
            e.printStackTrace();
        }


//        calendar.selectDate(today);
//
//        List<String> dates = new ArrayList<>();
//        dates.add("2015-6-10");
//        dates.add("2015-6-11");
//        dates.add("2015-6-15");
//        stringToDate(dates);


        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Log.d("data.time", date.getTime() + "");
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });
        return v;
    }

    public void stringToDate(List<String> dates) {
        for (int i = 0; i < dates.size(); i++)
            try {
                Date date = sdf.parse(dates.get(i));
                calendar.selectDate(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

    }

}
